<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="藏品订单编号" prop="collectionOrderNo">
        <el-input
          v-model="queryParams.collectionOrderNo"
          placeholder="请输入藏品订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品订单hashid" prop="collectionOrderNoHashId">
        <el-input
          v-model="queryParams.collectionOrderNoHashId"
          placeholder="请输入藏品订单hashid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品订单价格" prop="orderPrice">
        <el-input
          v-model="queryParams.orderPrice"
          placeholder="请输入藏品订单价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品id" prop="collectionId">
        <el-input
          v-model="queryParams.collectionId"
          placeholder="请输入藏品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品链上id" prop="collectionHashId">
        <el-input
          v-model="queryParams.collectionHashId"
          placeholder="请输入藏品链上id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品名称" prop="collectionName">
        <el-input
          v-model="queryParams.collectionName"
          placeholder="请输入藏品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品价格" prop="collectionPrice">
        <el-input
          v-model="queryParams.collectionPrice"
          placeholder="请输入藏品价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品作者" prop="collectionAuthorId">
        <el-input
          v-model="queryParams.collectionAuthorId"
          placeholder="请输入藏品作者"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品描述" prop="collectionDesc">
        <el-input
          v-model="queryParams.collectionDesc"
          placeholder="请输入藏品描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单过期时间" prop="expireTime">
        <el-input
          v-model="queryParams.expireTime"
          placeholder="订单过期时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="订单完成时间" prop="finishTime">
        <el-input
          v-model="queryParams.finishTime"
          placeholder="请输入订单完成时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="藏品展示id" prop="collectionShowId">
        <el-input
          v-model="queryParams.collectionShowId"
          placeholder="请输入藏品展示id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-input
          v-model="queryParams.orderStatus"
          placeholder="请输入订单状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['order:order:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['order:order:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['order:order:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['order:order:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="orderList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="藏品订单id" align="center" prop="collectionOrderId" />
      <el-table-column label="藏品订单编号" align="center" prop="collectionOrderNo" />
      <el-table-column
        label="藏品订单hashid"
        align="center"
        prop="collectionOrderNoHashId"
      />
      <el-table-column label="藏品订单价格" align="center" prop="orderPrice" />
      <el-table-column label="藏品id" align="center" prop="collectionId" />
      <el-table-column label="藏品链上id" align="center" prop="collectionHashId" />
      <el-table-column label="藏品名称" align="center" prop="collectionName" />
      <el-table-column label="藏品价格" align="center" prop="collectionPrice" />
      <el-table-column
        label="藏品图片地址"
        align="center"
        prop="collectionUrl"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.collectionUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="藏品作者" align="center" prop="collectionAuthorId" />
      <el-table-column
        label="藏品描述"
        align="center"
        prop="collectionDesc"
        show-overflow-tooltip="true"
      />
      <el-table-column label="订单过期时间" align="center" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, "{y}-{m}-{d} {h}:{i}:{s}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <span>{{
            scope.row.orderStatus == "0"
              ? "未支付"
              : scope.row.orderStatus == "1"
              ? "已支付"
              : scope.row.orderStatus == "3"
              ? "已取消"
              : scope.row.orderStatus == "4"
              ? "免费订单"
              : scope.row.orderStatus == "5"
              ? "被邀请订单"
              : ""
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单完成时间" align="center" prop="finishTime" />
      <el-table-column label="藏品展示id" align="center" prop="collectionShowId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['order:order:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['order:order:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改藏品订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="950px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="藏品订单编号" prop="collectionOrderNo">
          <el-input v-model="form.collectionOrderNo" placeholder="请输入藏品订单编号" />
        </el-form-item>
        <el-form-item label="藏品订单hashid" prop="collectionOrderNoHashId">
          <el-input
            v-model="form.collectionOrderNoHashId"
            placeholder="请输入藏品订单hashid"
          />
        </el-form-item>
        <el-form-item label="藏品订单价格" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入藏品订单价格" />
        </el-form-item>
        <el-form-item label="藏品id" prop="collectionId">
          <el-input v-model="form.collectionId" placeholder="请输入藏品id" />
        </el-form-item>
        <el-form-item label="藏品链上id" prop="collectionHashId">
          <el-input v-model="form.collectionHashId" placeholder="请输入藏品链上id" />
        </el-form-item>
        <el-form-item label="藏品名称" prop="collectionName">
          <el-input v-model="form.collectionName" placeholder="请输入藏品名称" />
        </el-form-item>
        <el-form-item label="藏品价格" prop="collectionPrice">
          <el-input v-model="form.collectionPrice" placeholder="请输入藏品价格" />
        </el-form-item>
        <el-form-item label="藏品图片地址">
          <image-upload v-model="form.collectionUrl" />
        </el-form-item>
        <el-form-item label="藏品作者" prop="collectionAuthorId">
          <el-input v-model="form.collectionAuthorId" placeholder="请输入藏品作者" />
        </el-form-item>
        <el-form-item label="藏品描述" prop="collectionDesc">
          <el-input v-model="form.collectionDesc" placeholder="请输入藏品描述" />
        </el-form-item>
        <el-form-item label="订单过期时间" prop="expireTime">
          <el-input v-model="form.expireTime" placeholder="订单过期时间" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="订单完成时间" prop="finishTime">
          <el-input v-model="form.finishTime" placeholder="请输入订单完成时间" />
        </el-form-item>
        <el-form-item label="藏品展示id" prop="collectionShowId">
          <el-input v-model="form.collectionShowId" placeholder="请输入藏品展示id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/order/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 藏品订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collectionOrderNo: null,
        collectionOrderNoHashId: null,
        orderPrice: null,
        collectionId: null,
        collectionHashId: null,
        collectionName: null,
        collectionPrice: null,
        collectionUrl: null,
        collectionAuthorId: null,
        collectionDesc: null,
        expireTime: null,
        userId: null,
        orderStatus: null,
        finishTime: null,
        collectionShowId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collectionOrderNo: [
          { required: true, message: "藏品订单编号不能为空", trigger: "blur" },
        ],
        collectionOrderNoHashId: [
          { required: true, message: "藏品订单hashid不能为空", trigger: "blur" },
        ],
        orderPrice: [
          { required: true, message: "藏品订单价格不能为空", trigger: "blur" },
        ],
        collectionId: [{ required: true, message: "藏品id不能为空", trigger: "blur" }],
        collectionHashId: [
          { required: true, message: "藏品链上id不能为空", trigger: "blur" },
        ],
        collectionName: [
          { required: true, message: "藏品名称不能为空", trigger: "blur" },
        ],
        collectionPrice: [
          { required: true, message: "藏品价格不能为空", trigger: "blur" },
        ],
        collectionUrl: [
          { required: true, message: "藏品图片地址不能为空", trigger: "blur" },
        ],
        collectionAuthorId: [
          { required: true, message: "藏品作者不能为空", trigger: "blur" },
        ],
        collectionDesc: [
          { required: true, message: "藏品描述不能为空", trigger: "blur" },
        ],
        expireTime: [
          { required: true, message: "订单过期时间不能为空", trigger: "blur" },
        ],
        userId: [{ required: true, message: "用户id不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询藏品订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then((response) => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        collectionOrderId: null,
        collectionOrderNo: null,
        collectionOrderNoHashId: null,
        orderPrice: null,
        collectionId: null,
        collectionHashId: null,
        collectionName: null,
        collectionPrice: null,
        collectionUrl: null,
        collectionAuthorId: null,
        collectionDesc: null,
        createTime: null,
        expireTime: null,
        userId: null,
        orderStatus: "0",
        finishTime: null,
        collectionShowId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.collectionOrderId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加藏品订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const collectionOrderId = row.collectionOrderId || this.ids;
      getOrder(collectionOrderId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改藏品订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.collectionOrderId != null) {
            updateOrder(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const collectionOrderIds = row.collectionOrderId || this.ids;
      this.$modal
        .confirm('是否确认删除藏品订单编号为"' + collectionOrderIds + '"的数据项？')
        .then(function () {
          return delOrder(collectionOrderIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "order/order/export",
        {
          ...this.queryParams,
        },
        `order_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
